package com.shanzmoo.core.util;

import cn.hutool.core.util.StrUtil;
import com.shanzmoo.base.constant.Constant;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;

import javax.servlet.http.HttpServletRequest;

/**
 * JWT 工具类
 *
 * @author： Gzhao 2021/3/10
 */
public class JwtUtils {

    private JwtUtils(){}

    /**
     * 从请求中获取到 jwt 信息
     * @param request
     * @return
     */
    public static String getJwtFromRequest(HttpServletRequest request) {
        String bearerToken = request.getHeader("Authorization");
        if (StrUtil.isNotBlank(bearerToken) && bearerToken.startsWith("Bearer ")) {
            return bearerToken.substring(7);
        }
        return null;
    }

    public static Claims getClaimsFromJwt(String jwt) {
        return Jwts.parser()
                .setSigningKey(Constant.JWT_KEY)
                .parseClaimsJws(jwt)
                .getBody();
    }
}
